package review;

import java.util.*;

public class HomeWork1 {
}

class HWA3{
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        HWA3 obj = new HWA3();
        int[] a = {1,2,3,4,5};
        try{
            System.out.println("请输入要访问的数组元素的序号:");
            int n = sc.nextInt();
            obj.visit(a,n);
            System.out.println("请输入要访问的数组元素的序号:");
            n = sc.nextInt();
            System.out.println("请输入要访问的数组元素的序号:");
            int m = sc.nextInt();
            obj.modify(a,n,m);
            System.out.println("请输入要访问的数组元素的序号:");
            n = sc.nextInt();
            obj.delete(a,n);
        }
        catch(Exception e){
            System.err.println(e.getMessage());
        }
    }

    void visit(int[] a,int b){
        if(a==null){
            throw new NullPointerException();
        }else if(a.length<=b||b<0){
            throw new ArrayIndexOutOfBoundsException("索引越界: " + b);
        }else {
            System.out.println("数组索引"+b+"对应的值:"+a[b]);
        }
    }

    void modify(int[] a,int b,int c){
        if(a==null){
            throw new NullPointerException();
        }else if(a.length<=b||b<0){
            throw new IllegalArgumentException("非法索引: " + b);
        }else {
            a[b] = c;
        }
    }

    void delete(int[] a,int b){
        if(a==null){
            throw new NullPointerException("数组为空");
        }else if(a.length<=b||b<0){
            throw new IllegalArgumentException("非法索引: " + b);
        }else {
            a[b] = 0;
        }
    }
}

class Book{
    String BookName;
    String AuthorName;
    double Price;

    public void print(){
        System.out.println("书名:"+this.BookName);
        System.out.println("作者:"+this.AuthorName);
        System.out.println("价格:"+this.Price);
    }
}

class LMSystem{
    static Scanner sc = new Scanner(System.in);
    static List<Book> books = new ArrayList<>();
    public static void main(String[] args) {
        LMSystem lms = new LMSystem();
        while(true){
            lms.menu(lms);
        }
    }

    void menu(LMSystem lms){
        System.out.println("1.添加图书 2.删除图书 3.查找图书 4.更新图书价格 5.显示所有图书 6.排序图书");
        int choice = sc.nextInt();
        switch (choice){
            case 1:lms.addBook(books);break;
            case 2:lms.deleteBook(books);break;
            case 3:lms.modifyBook(books);break;
            case 4:lms.updateBook(books);break;
            case 5:lms.printBook(books);break;
            case 6:lms.sortBook(books);lms.printBook(books);break;
            default:System.exit(0);;
        }
    }

    void addBook(List<Book> b){
        Book book = new Book();
        System.out.println("请输入书籍名称:");
        book.BookName = sc.next();
        System.out.println("请输入书籍作者:");
        book.AuthorName = sc.next();
        System.out.println("请输入书籍价格:");
        book.Price = sc.nextDouble();
        b.add(book);
    }
    void deleteBook(List<Book> b){
        System.out.println("请输入要删除的书籍名称:");
        String BookName = sc.next();
        for(Book book:b){
            if(book.BookName.equals(BookName)){
                b.remove(book);
                return;
            }
        }
        System.out.println("不存在这本书:"+BookName);
    }
    void modifyBook(List<Book> b){
        System.out.println("请输入要查找的书籍名称:");
        String BookName = sc.next();
        for(Book book:b){
            if(book.BookName.equals(BookName)){
                book.print();
                return;
            }
        }
        System.out.println("不存在这本书:"+BookName);
    }
    void updateBook(List<Book> b){
        System.out.println("请输入要更新的书籍名称:");
        String BookName = sc.next();
        for(Book book:b){
            if(book.BookName.equals(BookName)){
                System.out.println("请输入更新后的价格");
                book.Price = sc.nextDouble();
                return;
            }
        }
        System.out.println("不存在这本书:"+BookName);
    }
    void printBook(List<Book> b){
        System.out.println("所有书籍如下:");
        for(Book book:b){
            book.print();
        }
    }
    void sortBook(List<Book> b){
        Collections.sort(b, (o1, o2) -> {
            if (o1.Price < o2.Price) {
                return -1;
            } else if (o1.Price > o2.Price) {
                return 1;
            } else {
                return 0;
            }
        });
    }
}
